/*===========================================================================+
 |   Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA    |
 |                         All rights reserved.                              |
 +===========================================================================+
 |  HISTORY                                                                  |
 +===========================================================================*/
package xxhci.oracle.apps.XXIERP.ubot.webui;

import java.io.Serializable;

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.server.OADBTransaction;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.OAWebBeanConstants;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageTextInputBean;


/**
 * Controller for ...
 */
public class CreateCo extends OAControllerImpl {
    public static final String RCS_ID = "$Header$";
    public static final boolean RCS_ID_RECORDED = 
        VersionInfo.recordClassVersion(RCS_ID, "%packagename%");

    /**
     * Layout and page setup logic for a region.
     * @param pageContext the current OA page context
     * @param webBean the web bean corresponding to the region
     */
    public void processRequest(OAPageContext pageContext, OAWebBean webBean) {
        super.processRequest(pageContext, webBean);
        OAApplicationModule am = pageContext.getApplicationModule(webBean);

        String CStatus = "";
        CStatus = pageContext.getParameter("Status");
        String l_VehSpecHdrId = "";
        String l_OTSpecHdrId = "";
        String l_Person_Id = "";
        String l_LastName = "";
        String l_EmployeeNumber = "";
        String l_FirstName = "";
        String l_Grade="";
        String l_Org="";

        l_Person_Id = pageContext.getParameter("pPersonId");
        l_LastName = pageContext.getParameter("pLastName");
        l_FirstName = pageContext.getParameter("pFirstName");
        l_EmployeeNumber = pageContext.getParameter("pEmployeeNumber");
        l_Grade = pageContext.getParameter("pGrade");
        l_Org = pageContext.getParameter("pOrganization");
        String lScheduleMasterID = "";
        if (CStatus == "Create" || CStatus.equalsIgnoreCase("Create")) {
            if (!pageContext.isFormSubmission()) {
                Serializable[] initParams = { l_Person_Id };
                am.invokeMethod("CreateOTHdr", null);
                OAMessageTextInputBean OtHdrRefNumber = 
                    (OAMessageTextInputBean)webBean.findChildRecursive("OtReqRefNumber"); //OtReqPersonId
                     
                OAMessageTextInputBean OtReqPersonId = 
                    (OAMessageTextInputBean)webBean.findChildRecursive("OtReqPersonId");
                OAMessageTextInputBean EmpFirstName = 
                    (OAMessageTextInputBean)webBean.findChildRecursive("EmpFirstName");
                OAMessageTextInputBean EmpLastName = 
                    (OAMessageTextInputBean)webBean.findChildRecursive("EmpLastName");
                OAMessageTextInputBean EmpNumber = 
                    (OAMessageTextInputBean)webBean.findChildRecursive("EmpNumber");
                OAMessageTextInputBean EmpBandName = 
                    (OAMessageTextInputBean)webBean.findChildRecursive("EmpBandName");
                OAMessageTextInputBean EmpDeptName = 
                    (OAMessageTextInputBean)webBean.findChildRecursive("EmpDeptName");
                
                l_VehSpecHdrId = OtHdrRefNumber.getText(pageContext);
                if (l_VehSpecHdrId == null) {

                    OADBTransaction transaction = 
                        (OADBTransaction)am.getOADBTransaction();

                    OtHdrRefNumber.setValue(pageContext, 
                                            transaction.getSequenceValue("XXIERP.OT_SEQ_hdr_NO").toString());
                    OtReqPersonId.setValue(pageContext, l_Person_Id);
                    EmpFirstName.setValue(pageContext, l_FirstName);
                    EmpLastName.setValue(pageContext, l_LastName);
                    EmpNumber.setValue(pageContext, l_EmployeeNumber);
                    EmpDeptName.setValue(pageContext, l_Org);
                    EmpBandName.setValue(pageContext, l_Grade);
                }
                Serializable[] initParams1 = { l_VehSpecHdrId };
                System.out.println("35454546" + OtHdrRefNumber.getText());
                // OtHdrRefNumber.setValue(pageContext,l_Person_Id);
                am.invokeMethod("initOTLns", initParams1);

                /*OAAdvancedTableBean lVehSpecLnsRN =(OAAdvancedTableBean)webBean.findChildRecursive("VehSpecLnsRN");
                        lVehSpecLnsRN.setRendered(false);*/
            }
        } else if (CStatus == "View" || CStatus.equalsIgnoreCase("View")) {
            // l_VehSpecHdrId = pageContext.getParameter("pVehSpecHdrId");
            
            l_OTSpecHdrId= pageContext.getParameter("pHDRID");
            Serializable[] initParams = { l_OTSpecHdrId };
            // am.invokeMethod("CreateVehSpecHdr",null); 
            System.out.println("Header Id");
            System.out.println(l_Person_Id);
            am.invokeMethod("initOTHdr", initParams);
            // am.invokeMethod("CreateVehSpeclns",null); 
            OAMessageTextInputBean OtReqRefNumber = 
                (OAMessageTextInputBean)webBean.findChildRecursive("OtReqRefNumber"); //OtReqPersonId
                 
                 
            OAMessageTextInputBean C_OtReqAprvRemarks = 
                (OAMessageTextInputBean)webBean.findChildRecursive("OtReqAprvRemarks");
            C_OtReqAprvRemarks.getValue(pageContext).toString();
            if (C_OtReqAprvRemarks==null){
                
            }
            l_VehSpecHdrId = OtReqRefNumber.getText(pageContext);
            if (l_VehSpecHdrId == null) {

                OADBTransaction transaction = 
                    (OADBTransaction)am.getOADBTransaction();
               Number seqvalue = 
                   transaction.getSequenceValue("XXIERP.OT_SEQ_hdr_NO").intValue();
                 l_VehSpecHdrId=  seqvalue.toString();
                OtReqRefNumber.setValue(pageContext, 
                                        seqvalue);

            }
            Serializable[] initParams1 = { l_VehSpecHdrId };
            am.invokeMethod("initOTLns", initParams1);
            // OAMessageTextInputBean vSpecHeaderId = (OAMessageTextInputBean)webBean.findChildRecursive("SpecHeaderId");
            //vSpecHeaderId = (String)vSpecHeaderId.getValue(pageContext);
        }
    }

    /**
     * Procedure to handle form submissions for form elements in
     * a region.
     * @param pageContext the current OA page context
     * @param webBean the web bean corresponding to the region
     */
    public void processFormRequest(OAPageContext pageContext, 
                                   OAWebBean webBean) {
        super.processFormRequest(pageContext, webBean);
        OAApplicationModule am = pageContext.getApplicationModule(webBean);
        if (pageContext.getParameter("Apply") != null) {

            am.invokeMethod("apply");
            // OAMessageTextInputBean bosMeetingId = (OAMessageTextInputBean)webBean.findChildRecursive("BosMeetingId");
            //String l_minute_meeting_id = (String)bosMeetingId.getValue(pageContext);
            //TransactionUnitHelper.endTransactionUnit(pageContext, "empCreateTxn");
            OAException successMessage = 
                new OAException("Records has been Saved Successully.", 
                                OAException.CONFIRMATION);
            pageContext.putDialogMessage(successMessage);
            /*   pageContext.forwardImmediately("OA.jsp?page=/xxcto/oracle/apps/po/vs/webui/VehSpecCreationPG",//&pMasterID="+l_minute_meeting_id+"&Status="+Vstatus,
                                              null,
                                              OAWebBeanConstants.KEEP_MENU_CONTEXT,
                                              null,
                                              null,
                                              false, // retain AM
                                              OAWebBeanConstants.ADD_BREAD_CRUMB_NO);*/


        } else if (pageContext.getParameter("Cancel") != null) {
        
            am.invokeMethod("rollbackScheduleDetails");
            String Vstatus = "View";
            // TransactionUnitHelper.endTransactionUnit(pageContext, "vehicleDetailsCreateTxn");
            // retain AM
                pageContext.forwardImmediately("OA.jsp?page=/xxhci/oracle/apps/XXIERP/ubot/webui/Main_PG", 
                                               null, 
                                               OAWebBeanConstants.KEEP_MENU_CONTEXT, 
                                               null, null, false, 
                                               OAWebBeanConstants.ADD_BREAD_CRUMB_NO);

        }else if
            ("IOtToHour".equals(pageContext.getParameter(EVENT_PARAM))){//(pageContext.getParameter("IOtToHour")!= null){
            OAMessageTextInputBean C_Tohrs = 
                (OAMessageTextInputBean)webBean.findChildRecursive("IOtToHour");
            OAMessageTextInputBean C_FromHrs = 
                (OAMessageTextInputBean)webBean.findChildRecursive("HOtFromHour");
            OAMessageTextInputBean C_TotHrs = 
                (OAMessageTextInputBean)webBean.findChildRecursive("IOtTotalHour");    
                
         
           // Number totalHrs = 0;
             double Tohrs=Double.parseDouble(C_Tohrs.getValue(pageContext).toString());
            double FromHrs=Double.parseDouble(C_FromHrs.getValue(pageContext).toString());
            double totalHrs =Tohrs-FromHrs;
            C_TotHrs.setValue(pageContext,totalHrs);
            
            
        }
        if ("addRows".equals(pageContext.getParameter(EVENT_PARAM))) {
            OAMessageTextInputBean hspecHeaderId = 
                (OAMessageTextInputBean)webBean.findChildRecursive("OtReqRefNumber");

            OADBTransaction transaction = 
                (OADBTransaction)am.getOADBTransaction();
            Number seqvalue = 
                transaction.getSequenceValue("XXIERP.OT_SEQ_LINE_NO").intValue();
            String hspecHeaderIdval = 
                (String)hspecHeaderId.getValue(pageContext);
            // Serializable[] initParams = {hspecHeaderIdval};
            //  am.invokeMethod("initLubricantLines",initParams); 
            //am.invokeMethod("CreateVehSpeclns", null);
            // Serializable[] initParams = {hspecHeaderIdval};
            // am.invokeMethod("initVehSpecLns",initParams);
            am.invokeMethod("CreateOTlns", null);
            OAMessageTextInputBean lnsVehSpecHdrId = 
                (OAMessageTextInputBean)webBean.findChildRecursive("OtHdrRefNumber"); //OtReqPersonId

            lnsVehSpecHdrId.setValue(pageContext, hspecHeaderIdval);
            OAMessageTextInputBean lnslineId = 
                (OAMessageTextInputBean)webBean.findChildRecursive("lOtLineNum1");
            lnslineId.setValue(pageContext, seqvalue);
            //OAMessageTextInputBean vSpecHeaderId = (OAMessageTextInputBean)webBean.findChildRecursive("cSpecHeaderId");
            //vSpecHeaderId = (String)vSpecHeaderId.getValue(pageContext);
            // pageContext.putTransactionValue("VehSpecHdrId",lVehSpecHdrId );

        }
        /**
       * Apply and save for spec lines table
       */


    }


}
